/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMS;

import ImageCache.ImageCache;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author SamSung
 */
public class LoadImageFromRecord {

    private static RecordStore rs, rsb;
    private static final String IMAGES_RS = "ImageList";
    private final static String IMAGES_NAME = "ImageName";
    private static ImageCache images = ImageCache.getInstance();

    public static int numbOfRecord() {
        RecordEnumeration re = null;
        try {
            rsb = RecordStore.openRecordStore(IMAGES_RS, true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            System.out.println("Exception " + ex.toString());
        }
//        try {
//            re = rsb.enumerateRecords(null, null, false);
//        } catch (RecordStoreNotOpenException ex) {
//            ex.printStackTrace();
//        }
//        int n = re.numRecords();
        int n = 0;
        try {
            n = rsb.getNumRecords();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        try {
            rsb.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return n;
    }

    public static Image getImage(int i) {
        byte[] dt = null;
        try {
            rs = RecordStore.openRecordStore(IMAGES_NAME, true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            System.out.println("Exception " + ex.toString());
        }

        try {
            dt = rs.getRecord(i);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (InvalidRecordIDException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dt));
        String url = null;
        try {
            url = dis.readUTF();
        } catch (EOFException e) {
            System.out.println("Loi o day");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        Image image = null;
        if (url != null) {
            image = images.getImageResource(url);
        }
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return image;
    }
}
